//
//  CorrectionViewController.m
//  OrderSeat
//
//  Created by wangwei on 13-10-9.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "CorrectionViewController.h"

#define INPUT_MARK @"请输入您的纠错信息"

@interface CorrectionViewController()

@property (strong, nonatomic) UIColor *oldColor;

@end

@implementation CorrectionViewController

- (id)initWithShopId:(NSString*)shopId
{
    self = [super initWithNibClass:[CorrectionViewController class]];
    if (self) {
        _dataSource = [[CorrectionDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI
{
    [self createLeftButton];
    [self createNavigation:@"纠错信息"];
    
    [_backView drawBorder];
    [_tvContent drawBorderWidth:0.5];
    [_contactView drawBorderWidth:0.5];
    
    _oldColor = _tvContent.textColor;
    
    [self.view addTarget:self action:@selector(tapGestureAction:)];
}

- (void)leftButtonClick:(id)sender
{
    [self pushOrPopViewController:[ShopHomeViewController class]];
}

- (void)tapGestureAction:(id)sender
{    
    if ([_tvContact resignFirstResponder] || [_tvContent resignFirstResponder]) {
        if ([_tvContent.text isEqualToString:@""]) {
            _tvContent.text = INPUT_MARK;
            _tvContent.textColor = _oldColor;
        }
        
        [self.theScrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self.theScrollview setContentOffset:CGPointMake(0, 100) animated:YES];
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([_tvContent.text isEqualToString:INPUT_MARK]) {
        _tvContent.text = @"";
        _tvContent.textColor = [UIColor blackColor];
    }
    
    [self.theScrollview setContentOffset:CGPointMake(0, 40) animated:YES];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([_tvContent.text isEqualToString:@""]) {
        _tvContent.text = INPUT_MARK;
        _tvContent.textColor = _oldColor;
    }
}

- (IBAction)TextField_DidEndOnExit:(id)sender
{
    // 隐藏键盘.
    [sender resignFirstResponder];
    [self.theScrollview setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (IBAction)submitAction:(id)sender
{
    if (![self checkForm])return;
    
    [_tvContact resignFirstResponder];
    [_tvContent resignFirstResponder];
    
    NSString *content = _tvContent.text;
    NSString *contact = _tvContact.text;
    
    NSString *url = [NSString stringWithFormat:API_SHOP_CHANGE_ERR,_shopId,content,contact];
    [_dataSource submitWithURL:url];
    [SVProgressHUD showWithStatus:@"正在提交纠错信息..."];
}

- (BOOL)checkForm
{
    NSString *content = _tvContent.text;
    NSString *contact = _tvContact.text;
    if ([content isEqualToString:@""]) {
        [AppDelegate showMessage:@"纠错内容不能为空！"];
        return NO;
    }
    if ([contact isEqualToString:@""]) {
        [AppDelegate showMessage:@"联系方式不能为空！"];
        return NO;
    }
    
    return YES;
}

@end

//=========================================================================================
#pragma ===================数据源类实现======================================================
//=========================================================================================

@implementation CorrectionDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if (self) {
        self.viewController = viewController;
    }
    return self;
}

- (void)submitWithURL:(NSString *)url
{
    [self get:url];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@", request.responseString);
    NSDictionary *json = [request.responseData JSONValue];
    if ([[json objectForKey:@"error_code"] intValue] != 0) {
        return;
    }
        
    NSDictionary *data = [json objectForKey:@"data"];
    if ([[data objectForKey:@"state"] intValue] == 1) {
        [SVProgressHUD dismissWithSuccess:@"您的纠错信息提交成功!"];
        [_viewController performSelector:@selector(leftButtonClick:) withObject:nil afterDelay:1];
        return;
    }
    
    [SVProgressHUD dismissWithSuccess:@"您的纠错信息提交失败!"];
}

@end


